//
//  StatusPictureView.swift
//  微博项目
//
//  Created by zhaoyinpeng on 16/3/15.
//  Copyright © 2016年 zhaoyinpeng. All rights reserved.
//

import UIKit

private let picturCellId = "picturCellId"

private let picturCellMargin: CGFloat = 5
class StatusPictureView: UICollectionView {
    
    //集合视图显示图片数据源 + 获取图片的数量
    var imageURLs: [NSURL]? {
        didSet {
            //.根据配图的数量 计算配图视图的大小
            let pSize = calculatePictureViewSize()
            //更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            
            //.设置测试文字
            testLable.text = "\(imageURLs?.count ?? 0)"
            
            //.刷新视图
            reloadData()
        }
    }
    
    
    //.重写构造方法
    init() {
        
        //必须调用指定的构造函数
        //.布局对象 必须是  流水 布局对象
        let layout = UICollectionViewFlowLayout()
        
        //设置最小间距
        layout.minimumInteritemSpacing = picturCellMargin
        layout.minimumLineSpacing = picturCellMargin
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        //.注册cell可重用标识符
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: picturCellId)
        //.设置数据源
        self.dataSource = self
        self.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func calculatePictureViewSize() -> CGSize {
        
        let imageCount = imageURLs?.count ?? 0
        
        let maxWidth = ScreenWidth - 2 * StatusCellMargin
        
        let itemWidth = (maxWidth - 2 * picturCellMargin)/3
        
        //.获取layout对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //.设置itemSize
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1{
            //.给定图片原尺寸大小
            //.TODO: 比较困难 后续完善
            let imageSize = CGSizeMake(150, 100)
            //. 需要设置 itmeSize
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            let w = itemWidth * 2 + picturCellMargin
            return CGSizeMake(w, w)
        }
        
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let pHeight = row * itemWidth + (row - 1) * picturCellMargin
        
        
        return CGSizeMake(maxWidth, pHeight)
        
    }
    
    
    
    //.MARK: 设置UI
    private func setupUI() {
        addSubview(testLable)
       testLable.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self.center)
        }
    }
    
    private lazy var testLable: UILabel = UIFactory.labelFactory(UIColor.redColor(), fontSize: 40, text: "")
}

//.数据源协议
extension StatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(picturCellId, forIndexPath: indexPath) as! PictureCell
        
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
    
}

class PictureCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
            
        }
    }
    
    //.重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //.MARK: 设置UI界面
    private func setupUI() {
        contentView.addSubview(iconView)
        //.设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
            
        }
    }
    
    //.懒加载视图
    private lazy var iconView: UIImageView = {
        let iv = UIImageView()
        
        iv.contentMode = .ScaleAspectFill
        
        //.手写代码 需要手动设置 剪裁
        iv.clipsToBounds = true
        
        return iv
    }()
}

